<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>用户角色配置</title>
<script type="text/javascript">
	var $userGrid;
	var $dataRoleDataGrid;
	var $roleDataGrid;
	var selectTableRow;//选中的行数据
	$(function() {
		$userGrid = $("#userGrid").datagrid($.extend(datagridOption, {
			title : '用户信息',
			url : "${ctx}/basic/userRoles/userDataGrid",
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'loginNo',
				title : '登录账号',
				width : 100,
				sortable : true
			}, {
				field : 'trueName',
				title : '姓名',
				width : 100,
				sortable : true
			} ] ],
			onClickRow : function(rowIndex, rowData) {
				$.easyui.loading({ msg: "正在加载...", locale: "#roleLayout" });
				selectTableRow = rowData;
				$("#cc").layout("panel", "center").panel({
					title : "用户【" + rowData.trueName + "】角色"
				});
				$("#cc").layout("panel", "east").panel({
					title : "用户【" + rowData.trueName + "】数据角色"
				});
				fn_refreshRoles();
			}
		}));

		$dataRoleDataGrid = $("#dataRoleDataGrid").datagrid($.extend(datagridOption, {
			url : "${ctx}/basic/userRoles/dataRolesDataGrid",
			title : "",
			toolbar : '#datarolett',
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'dataRoleName',
				title : '数据角色名称',
				width : 120,
				sortable : true
			}, {
				field : 'dataRoleNo',
				title : '数据角色编号',
				width : 100,
				sortable : true
			} ] ],
			onClickRow : function(rowIndex, rowData) {
			}
		}));
		$roleDataGrid = $("#roleDataGrid").datagrid($.extend(datagridOption, {
			title : "",
			url : "${ctx}/basic/userRoles/rolesDataGrid",
			toolbar : '#rolett',
			showFooter : false,
			pagination:false,
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'roleName',
				title : '角色名称',
				width : 176,
				sortable : true
			}, {
				field : 'roleNo',
				title : '角色编号',
				width : 170,
				sortable : true
			} ] ],
			onClickRow : function(rowIndex, rowData) {
			}
		}));
		$userGrid.datagrid('enableFilter');
		/* $dataRoleDataGrid.datagrid('enableFilter');
		$roleDataGrid.datagrid('enableFilter'); */
		$("#userRolesAdd").click(function() {
			fn_userRolesSave();
		});
		/* $("#search").click(function() {
			var search_LIKE_dataRoleName = $("#search_LIKE_dataRoleName").val();
			$userGrid.datagrid('load', {
				search_LIKE_dataRoleName : search_LIKE_dataRoleName
			});
		}); */
		
		//角色搜索按钮
		$("#rolesearch").click(function() {
			var search_LIKE_roleName = $("#search_LIKE_roleName").val();
			$roleDataGrid.datagrid('load', {
				search_LIKE_roleName : search_LIKE_roleName
			});
			/* fn_refreshRoles();  */
		});
		//数据角色查询按钮
		$("#datarolesearch").click(function() {
			var search_LIKE_dataRoleName = $("#search_LIKE_dataRoleName").val();
			$dataRoleDataGrid.datagrid('load', {
				search_LIKE_dataRoleName : search_LIKE_dataRoleName
			});
			/* fn_refreshRoles(); */
		});
	});
	function fn_save() {
		$("#fm").form("submit", {
			url : "${ctx}/basic/dataRole/update",
			success : function(data) {
				$userGrid.datagrid("load");
				$.messager.show({
					msg : "保存成功",
					icon : "info",
					position : "bottomRight"
				});
				$("#dlg").dialog('close');
			}
		});
	}
	function fn_userRolesSave() {
		var dataRoleCheckedData = $dataRoleDataGrid.datagrid("getChecked");
		var roleCheckedData = $roleDataGrid.datagrid("getChecked");
		if (selectTableRow == null) {
			$.messager.show({
				msg : "请先选定用户",
				icon : "warning",
				position : "bottomRight"
			});
			return false;
		}
		/* if (dataRoleCheckedData == null || dataRoleCheckedData.length < 1) {
			$.messager.show({
				msg : "请先勾选需要分配的数据角色",
				icon : "warning",
				position : "bottomRight"
			});
			return false;
		}
		if (roleDataGrid == null || roleDataGrid.length < 1) {
			$.messager.show({
				msg : "请先勾选需要分配的角色",
				icon : "warning",
				position : "bottomRight"
			});
			return false;
		} */
		var dataRoleIds = "";
		for (var i = 0; i < dataRoleCheckedData.length; i++) {
			if (dataRoleCheckedData[i].id) {//如果为子节点
				dataRoleIds += (dataRoleCheckedData[i].id + ",");
			}
		}
		var roleIds = "";
		for (var i = 0; i < roleCheckedData.length; i++) {
			if (roleCheckedData[i].id) {//如果为子节点
				roleIds += (roleCheckedData[i].id + ",");
			}
		}
		$.post("${ctx}/basic/userRoles/addRoles", {
			userId : selectTableRow.id,
			roleIds : roleIds,
			dataRoleIds : dataRoleIds
		}, function(result) {
			$.messager.show({
				msg : result.msg,
				icon : "info",
				position : "bottomRight"
			});
			if (result.success) {
				//$userGrid.datagrid("load");
			}
		}, 'json');
	}

	function fn_refreshRoles() {
		if (!selectTableRow) {
			$.messager.show({
				msg : "请先选定用户",
				icon : "warning",
				position : "bottomRight"
			});
			return false;
		}
		/* $dataRoleDataGrid.datagrid('enableFilter');
		$roleDataGrid.datagrid('enableFilter'); */
		$.get("${ctx}/basic/userRoles/jsonUserRoles?userId=" + selectTableRow.id, function(result) {
			$.easyui.loaded("#roleLayout");
			if (result.success && result.obj) {
				$dataRoleDataGrid.datagrid("clearChecked");
				if (result.obj[0]) {
					for (var i = 0; i < result.obj[0].length; i++) {
						var node = $dataRoleDataGrid.datagrid("getRowIndex", result.obj[0][i].id);
						if (node > -1) {
							$dataRoleDataGrid.datagrid("checkRow", node);
						}
					}
				}
				$roleDataGrid.datagrid("clearChecked");
				if (result.obj[1]) {
					for (var i = 0; i < result.obj[1].length; i++) {
						var node = $roleDataGrid.datagrid("getRowIndex", result.obj[1][i].id);
						if (node > -1) {
							$roleDataGrid.datagrid("checkRow", node);
						}
					}
				}
			}
			$.messager.show({
				msg : result.msg,
				icon : "info",
				position : "bottomRight"
			});
		}, 'json');
	}
</script>
</head>

<body class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center'" data-options="fit:true" style="padding: 1px;">
		<div id="cc" class="easyui-layout" data-options="fit:true" style="width: 350px; height: 480px;">
			<div data-options="region:'west', split: true, collapsible:true" style="padding: 1px; witdh: 300px">
				<table id="userGrid">
				</table>
				<div id="tt" class="row">
					<!-- <span>用户姓名：</span> <input type="text" id="search_LIKE_trueName"
						style="margin-left: 2px; margin-right: 2px; padding-left: 2px; padding-right: 3px;" />
					<a id="search" class="easyui-linkbutton" data-options="plain: true, iconCls: 'icon-hamburg-zoom'">查询</a>
					<div class="dialog-tool-separator"></div> -->
					<div class="col-md-12">
						<shiro:hasPermission name="userRoles:update">
							<button id="userRolesAdd" type="button" class="btn btn-primary">
								<span class="glyphicon glyphicon-save"></span> 保存配置
							</button>
						</shiro:hasPermission>
					</div>
				</div>
			</div>
			<div id="roleLayout" data-options="region:'center', title:'角色', split: true, collapsible:true" style="padding: 1px; witdh: 300px">
				<table id="roleDataGrid">
				</table>
				<div id="rolett" class="easyui-toolbar">
					 <span>角色名称：</span> <input type="text" id="search_LIKE_roleName"
						style="margin-left: 2px; margin-right: 2px; padding-left: 2px; padding-right: 3px;" />
					<a id="rolesearch" class="easyui-linkbutton" data-options="plain: true, iconCls: 'icon-hamburg-zoom'">查询</a> 
				</div>
			</div>
			<div data-options="region:'east', title:'数据角色', split: true, collapsible:true" style="padding: 1px; witdh: 300px">
				<table id="dataRoleDataGrid">
				</table>
				<div id="datarolett" class="easyui-toolbar">
					<span>数据角色名称：</span> <input type="text" id="search_LIKE_dataRoleName"
						style="margin-left: 2px; margin-right: 2px; padding-left: 2px; padding-right: 3px;" />
					<a id="datarolesearch" class="easyui-linkbutton" data-options="plain: true, iconCls: 'icon-hamburg-zoom'">查询</a>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
